<?php
include '../../../wp-blog-header.php';
include './config.php';

if(!empty($_GET['sendto']))
{
  $url = decrypt($_GET['sendto'],'1b2u3z4z5r6');
  $url = explode('?',$url);
  $red_link = get_bloginfo('url').$url['0'];
  $url = explode('&',$url['1']);
  array_pop($url);
  array_pop($url);
  $url = implode('&',$url);
  $link = $red_link.'?'.$url;
  header("Location: $link");
}

if(isset($_POST['submit']))
{
  function trim_value(&$value) 
  { 
    $value = trim($value); 
  } 
   
  if(!empty($_POST['name'])) { $name = $_POST['name']; }
  if(!empty($_POST['slug'])) { $slug = $_POST['slug']; }
  if(!empty($_POST['user_email'])) { $user_email = $_POST['user_email']; }

  if(!empty($_POST['email_ids']))
  {
    $email_ids = $_POST['email_ids'];
    
    $pos = strpos($email_ids, ',');
    if ($pos === false) {
      $email_ids = array($email_ids);
    }
    else{
      $email_ids = explode(',',$email_ids);
      array_walk($email_ids, 'trim_value');
      $email_ids = array_unique($email_ids);
    }

    foreach($email_ids as $value)
    {
      if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $value))
      {
        $valid_email_ids[] = $value; 
      }
    }
  }
  
  $valid_email_ids = array_slice($valid_email_ids, 0, 50);
  
  foreach($valid_email_ids as $val)
  {
    if(!empty($val))
    {
      $link = 'http://buzzr.in/?utm_source=invite&utm_medium=email&utm_campaign=email%2Binvitations&ref='.$slug.'&visitors_email='.$val;
      
      $mail_queue = new Mail_Queue($db_options, $mail_options);

      $from = 'Buzzr.in';
      $to = $val;
      $message = '<div id="body" style="width: 500px; border: solid; border-color: #FFFFFF; background-color: #FFFFFF;">
      <div id="logo" style="text-align: left">
      	<img style="margin-left: 178px; margin-top: 10px;" height="40" src="http://buzzr.in/wordpress/wp-content/uploads/2011/01/buzzrin-logo.png" />
      </div>
      <div id="content" style="width: auto; margin: 20px; padding: 5px; background-color: #D0F0FB; font-size: 14px; color:#1BA3DF;">
      Hello,<br /><br />
      <i><b>'.$name.'</b>('.$user_email.')</i> has invited you to Buzzr.in.<br /><br />
      Here is an invitation to check out Buzzr.in, a website that brings you great offers to help you save money on going out, shopping and eating in your city. Just click on the invitation link below to visit the site. To make things better you will be rewarded 1000 points on registration and I will get 300 points for referring you!<br /><br />
      
      <h3 style="display: inline;"><a target="_blank" href="'.$link.'">Click here to check out Buzzr.in</a></h3><br /><br />
      Thank You!
      
      <font><i><br /><br />This email was sent to you by ('.$name.') through Buzzr.in<br />
      If this message does not apply to you please ignore the email Thank You! 
      </i></font>
      </div>
      
      </div>';
      
      $hdrs = array( 'From'    => $from,
                     'To'      => $to,
                     'Subject' => 'Buzzr.in: Invitation from '.$name );
      
      /* we use Mail_mime() to construct a valid mail */
      $mime =& new Mail_mime();
      $mime->setHTMLBody($message);
      $body = $mime->get();
      $hdrs = $mime->headers($hdrs);
      
      
      /* Put message to queue */
      $mail_queue->put($from, $to, $hdrs, $body);
      
      
      
      /*
      $msg=
      '<div id="body" style="width: 500px; border: solid; border-color: #FFFFFF; background-color: #FFFFFF;">
      <div id="logo" style="text-align: left">
      	<img style="margin-left: 178px; margin-top: 10px;" height="40" src="http://buzzr.in/wordpress/wp-content/uploads/2010/04/buzzrlogo.gif" />
      </div>
      <div id="content" style="width: auto; margin: 20px; padding: 5px; background-color: #D0F0FB; font-size: 14px; color:#1BA3DF;">
      Hello,<br /><br />
      <i><b>'.$name.'</b>('.$user_email.')</i> has invited you to Buzzr.in.<br /><br />
      Buzzr brings you the latest deals, offers & coupons, from brands which you already shop at!<br />
      
      <h3 style="display: inline;"><a target="_blank" href="'.$link.'">Click here to check out Buzzr.in.</a></h3><br />
      Thank You!
      
      <font><i><br /><br />This email was sent to you on behalf of one of ('.$name.')<br />
      If this message does not apply to you please ignore the email Thank You! 
      </i></font>
      </div>
      
      </div>';
      $subject = 'Buzzr.in: Invitation from '.$name;
      send_html_mail($val, $subject , $msg, 'Buzzr.in<info@enthrallmedia.com>');
      $link = $_SERVER['HTTP_REFERER'].'?sent=1';
      header("Location: $link");
      */
    }
  }
  
  $red_link = $_SERVER['HTTP_REFERER'].'?sent=1';
  header("Location: $red_link");  
}
?>